package com.instalinks.android.apps.smspostservice;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.instalinks.android.apps.smspostservice.helper.AppConfig;
import com.instalinks.android.apps.smspostservice.helper.Constants;
import com.instalinks.android.lib.AppHelper;
import com.instalinks.android.lib.LibConstants;

public abstract class AppTabBaseActivity extends Activity {
	protected AppConfig mAppCfg;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mAppCfg = new AppConfig(this);

		if(!mAppCfg.isBaseServiceRunning()){
			jumpToError(LibConstants.Error.BASE_APP_ERR);
		}
		checkForConfig();
		
		this.doInitialize();
		this.doPostInitialize();
	}
	
	protected abstract void doInitialize();
	
	protected abstract void doPostInitialize();

	protected void closeMe(){
		this.finish();
	}
	
	private void checkForConfig() {

		if (mAppCfg.isConfigExists())
			return;

		boolean result = mAppCfg.updateConfigFromHost();
		// result = false; //for testing
		if (!result) {
			jumpToError(Constants.ERR_CONFIG_DOWNLOAD);
		}
	}

	private void jumpToError(String error) {
		Intent i = new Intent();
		i.setClassName(getApplicationContext().getPackageName(),
				ErrorActivity.class.getName());

		Bundle bundle = new Bundle();
		bundle.putString("error", error);
		i.putExtras(bundle);

		startActivity(i);
		this.finish();
	}

	protected boolean reloadConfigFromHost() {
		boolean result = mAppCfg.updateConfigFromHost();
		return result;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.app_menu, menu);

		handleMenuDisplay(menu);
		
		return true;
	}

	protected void handleMenuDisplay(Menu menu){
	}

	@Override
	public void onBackPressed() {
		if (!(this instanceof HomeActivity)) {
			this.finish();
			Intent i = new Intent();
			i.setClassName(getApplicationContext().getPackageName(),
					HomeActivity.class.getName());
			startActivity(i);
		}
		return;
	}
	
//	@Override
//	public void onAttachedToWindow() {
//	    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
//	    super.onAttachedToWindow();
//	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		String activityClassName = HomeActivity.class.getName();

		// Handle item selection
		switch (item.getItemId()) {
		case R.id.mnuHome:
			activityClassName = HomeActivity.class.getName();
			break;
		case R.id.mnuSettings:
			activityClassName = SettingsActivity.class.getName();
			break;
		case R.id.mnuUserConfig:
			activityClassName = UserConfigActivity.class.getName();
			break;
		case R.id.mnuWeb:
			activityClassName = WebActivity.class.getName();
			break;
		case R.id.mnuExit:
			AppHelper.killApp(true);
			break;
		default:
			return handleMenuClick(item);
		}

		switchActivity(activityClassName);
		return true;
	}
	
	protected boolean handleMenuClick(MenuItem item){
		return super.onOptionsItemSelected(item);
	}
	
	private void switchActivity(String targetActivityClassName){
		this.finish();

		Intent i = new Intent();
		i.setClassName(getApplicationContext().getPackageName(), targetActivityClassName);
		startActivity(i);
	}
	
	// <editor-fold defaultstate="collapsed" desc="user-description">
	protected class SubTask extends AsyncTask<Void, Void, Boolean> {
		ProgressDialog pd;
		String onProgressMessage = "";

		public SubTask(String onProgressMessage) {
			this.onProgressMessage = onProgressMessage;
		}

		@Override
		protected void onPreExecute() {
			pd = ProgressDialog.show(AppTabBaseActivity.this, "",
					onProgressMessage);
		}

		@Override
		protected Boolean doInBackground(Void... params) {
			return doBackgroundWork();
		}

		@Override
		protected void onPostExecute(Boolean result) {

			pd.dismiss();
			pd.cancel();

			if (result) {
				doOnSuccess();
			} else {
				doOnFailure();
			}
		}
	}

	protected abstract boolean doBackgroundWork();

	protected abstract void doOnSuccess();

	protected abstract void doOnFailure();
	// </editor-fold>
}
